package com.feicheng.iou.service.impl.weChat;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.controller.weChat.util.WechatPayConfig;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.weChatService.WeChatPayService;
import com.feicheng.iou.util.HttpRequest;
import com.feicheng.iou.weChatSdk.WXPayConstants;
import com.feicheng.iou.weChatSdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class WeChatPayServiceImpl implements WeChatPayService {
    /**
     * @return ResultInfoV110
     * @description 下单并且支付,主要是做两次签名
     */
    @Override
    public ResultInfoV110 pay(String openid,String attachPaymentId,String totalFee) {
        log.info("进入WeChatPayServiceImpl的pay方法,参数openid为"+openid+",参数还款期数"+attachPaymentId+",参数还款金额为"+totalFee);
        //TODO totalFee乘以100变成分
        Map<String, String> paraMap = new HashMap<String, String>();
        try{
            //封装统一下单信息
            paraMap.put("appid", WechatPayConfig.APPID);
            paraMap.put("attach", attachPaymentId);
            paraMap.put("body", "支付分期");
            paraMap.put("mch_id", WechatPayConfig.MCH_ID);
            paraMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paraMap.put("openid", openid);
            paraMap.put("out_trade_no", UUID.randomUUID().toString().replaceAll("-", ""));//订单号
            paraMap.put("spbill_create_ip", WechatPayConfig.SPBILL_CREATE_IP);
            paraMap.put("total_fee","1"); //TODO 这里先写0.01元
            paraMap.put("trade_type", WechatPayConfig.TRADE_TYPE);
            paraMap.put("notify_url",WechatPayConfig.NOTIFY_URL);// 此路径是微信服务器调用支付结果通知路径
            String sign = WXPayUtil.generateSignature(paraMap, WechatPayConfig.KEY, WXPayConstants.SignType.MD5);
            paraMap.put("sign", sign);
            String xml = WXPayUtil.mapToXml(paraMap);//将所有参数(map)转xml格式

            //统一下单接口 https://api.mch.weixin.qq.com/pay/unifiedorder
            String unifiedorder_url = WechatPayConfig.UNIFIEDORDER_URL;
            //发送请求
            String xmlStr = HttpRequest.sendPost(unifiedorder_url,xml);
            log.info("xmlStr为"+xmlStr);

            //以下内容时返回前端页面的json数据
            String prepay_id ="";

            if (xmlStr.indexOf("SUCCESS") != -1) {
                Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
                prepay_id = (String) map.get("prepay_id");
            }
            Map<String, String> payMap = new HashMap<String, String>();
            //二次签名
            payMap.put("appId", WechatPayConfig.APPID);
            payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp()+"");
            payMap.put("nonceStr", WXPayUtil.generateNonceStr());
            payMap.put("signType", WechatPayConfig.SIGN_TYPE);
            payMap.put("package", "prepay_id=" + prepay_id);
            String paySign = WXPayUtil.generateSignature(payMap, WechatPayConfig.KEY, WXPayConstants.SignType.MD5);
            payMap.put("paySign", paySign);
            log.info("payMap为"+payMap);
            return ResultInfoV110.success(payMap);
        }catch (Exception e){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001,"微信支付"));
        }
    }
}
